package gamemodel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import gamemodel.clock.RollOverCounter;

public class ScoreBoard {
	
	private RollOverCounter timeLeft; //represents time remaining on the score board
	private int teamAScore; //represents teamA score
	private int teamBScore; //represents teamB score 
	private int maxScore; //represents the maximum score to be reached
	private int state ; //represents the state of the scoreboard 
	private int timeLimit ; // represents the time l
	
	//static constants representing each team 
	public static final int TEAM_A = 0; //left side of playground
	public static final int TEAM_B = 1; //right side of playground
	public static final int TEAM_SPECTATOR = 2; //only watches the game
	
	//static constants representing state of scoreboard
	public static final int IN_PROGRESS = 0; 
	public static final int TIME_EXPIRED = 1;
	public static final int TEAM_A_WIN = 2; 
	public static final int TEAM_B_WIN = 3; 
	
	
	public ScoreBoard(int minutes, int maxScore){
		this.teamAScore = 0; 
		this.teamBScore = 0; 
		this.maxScore = maxScore;
		timeLeft = new RollOverCounter(minutes); 
		this.state = ScoreBoard.IN_PROGRESS; 
	}
	
	/**
	 * Performs a tick on the time remaining on the clock, only if game state is IN_PROGRESS 
	 */
	public void tick(){
		if (state == ScoreBoard.IN_PROGRESS) {
			timeLeft.tick();
			
			if (this.timeLeft.isTimeExpired()){
				state = ScoreBoard.TIME_EXPIRED; 
			}
		}	
	}
	
//	public boolean isTimeExpired(){
//		return timeLeft.isTimeExpired(); 
//	}
	
	public void setMaxScore (int score){
		this.maxScore = score; 
	}
	
	public void setTimeLimit (int minutes){
		this.timeLimit = minutes;
		this.timeLeft = new RollOverCounter(timeLimit); 
	}
	
	public int getState(){
		return this.state; 
	}
	
	/**
	 * Determines the score for the team specified 
	 * @param team Can either be constants TEAM_A or TEAM_B
	 * @return the score of the team specified. If team is invalid, returns -1. 
	 */
	public int getScore(int team){
		//error checking 
		if (team != TEAM_A && team != TEAM_B){
			return -1; 
		}
		
		if (team == TEAM_A){
			return this.teamAScore;
		} else if (team == TEAM_B){
			return this.teamBScore; 
		}
		
		return -1; 
	}
	
	/**
	 * This adds a score to the team specified 
	 * @param team - corresponds to the static team constants defined in
	 *  			 this class (ie. TEAM_A = 0 and TEAM_B = 1) 
	 */
	public void addScore(int team){
		//error check - make sure game is still in progress 
		if (this.state != ScoreBoard.IN_PROGRESS){
			return; 
		}
		
		if (team == TEAM_A && teamAScore < this.maxScore){
			teamAScore++; 
			if (this.teamAScore == maxScore){
				this.state = ScoreBoard.TEAM_A_WIN; 
			}
		} else if (team == TEAM_B && teamBScore < this.maxScore){
			teamBScore++; 
			if (this.teamBScore == maxScore){
				this.state = ScoreBoard.TEAM_B_WIN; 
			}
		}
		
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append(this.timeLeft); 
		sb.append(", TeamA: "); 
		sb.append(this.teamAScore); 
		sb.append(", Team B: "); 
		sb.append(this.teamBScore); 
		sb.append(", maxScore: "); 
		sb.append(this.maxScore); 
		
		return sb.toString();
		
	}
	
	public void reset(int minute, int maxScore){
		this.timeLeft = new RollOverCounter(minute); 
		this.maxScore = maxScore; 
		this.state = ScoreBoard.IN_PROGRESS; 
	}
	
	public void paint(Graphics2D g){
		g.setColor(Color.BLACK); 
		g.drawString(this.toString() ,30, 60);
	}
	
	public static void main(String args[]){
		ScoreBoard sb = new ScoreBoard(3, 3); 
		System.out.println(sb);
		sb.addScore(ScoreBoard.TEAM_A); 
		System.out.println(sb); 
		sb.tick(); 
		System.out.println(sb); 
		sb.addScore(ScoreBoard.TEAM_B); 
		System.out.println(sb); 
	}
}
